import { XRFrame } from "three"
import Engine from "./Engine"

export default class EngineTick {
  ticking: boolean
  engine: Engine
  onErrorCallback: Function
  constructor(engine: Engine, render: THREE.WebGLRenderer, onErrorCallback: Function) {
    this.engine = engine
    this.onErrorCallback = onErrorCallback
    this.ticking = !1
    render.setAnimationLoop(this.tick.bind(this))
  }
  start() {
    this.ticking = !0
  }
  stop() {
    this.ticking = !1
  }
  tick(time: number, frame?: XRFrame) {
    if (this.ticking)
      try {
        this.engine.setXrFrame(frame)
        this.engine.tick()
      } catch (e) {
        this.stop()
        this.onErrorCallback(e)
      }
  }
}
